/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-web-praise nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.lpitools.gwtgui.client.components.graphpanel;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;

public class SRIGraphTable extends CaptionPanel{

	private CellTable<ResultModel> dataGrid;
	private final ListDataProvider<ResultModel> dataProvider;

	private List<ResultModel> dataList;

	public SRIGraphTable(){
		//create datagrid for properties
		dataGrid = new CellTable<ResultModel>();

		//create single selection model for this datagrid
		final SingleSelectionModel<ResultModel>  selectionModel = new SingleSelectionModel<ResultModel>();

		//add selectionModel to the datagrid for when checkbox is clicked
		dataGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<ResultModel> createCheckboxManager());

		//create dataProvider for the grid
		dataProvider = new ListDataProvider<ResultModel>();

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(dataGrid);

		//create list of properties
		dataList = new ArrayList<ResultModel>();

		//add data to the list in the provider
		List<ResultModel> list = dataProvider.getList();
		for (ResultModel sort : dataList) {
			list.add(sort);
		}

		//create sort handler and add it to the grid
		ListHandler<ResultModel> sortHandler = new ListHandler<ResultModel>(list);

		dataGrid.addColumnSortHandler(sortHandler);

		//create column for the names of the properties
		Column<ResultModel, String> nameColumn = new Column<ResultModel, String>(new TextCell()) {
			@Override
			public String getValue(ResultModel object) {
				return object.getName();
			}
		};
		nameColumn.setSortable(true);

		//set the comparator for the nameColumn
		sortHandler.setComparator(nameColumn, new Comparator<ResultModel>() {
			public int compare(ResultModel o1, ResultModel o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});

		//add name column to the grid
		dataGrid.addColumn(nameColumn, "Step Title");	

		//build up the constants column
		Column<ResultModel, String> valueColumn = new Column<ResultModel, String>(new TextCell()) {
			@Override
			public String getValue(ResultModel object) {
				return object.getValueString();
			}
		};
		valueColumn.setSortable(true);

		//set the comparator for the constantsColumn
		//even though we display the string representation of the value we compare on the double representation
		sortHandler.setComparator(valueColumn, new Comparator<ResultModel>() {
			public int compare(ResultModel o1, ResultModel o2) {
				return o1.getValue().compareTo(o2.getValue());
			}
		});	

		dataGrid.addColumn(valueColumn, "Step Time in milliseconds");

		//create column for the names of the properties
		Column<ResultModel, Number> rankColumn = new Column<ResultModel, Number>(new NumberCell()) {
			@Override
			public Number getValue(ResultModel object) {
				return object.getOrder();
			}
		};
		rankColumn.setSortable(true);

		//set the comparator for the nameColumn
		sortHandler.setComparator(rankColumn, new Comparator<ResultModel>() {
			public int compare(ResultModel o1, ResultModel o2) {
				return o1.getOrder().compareTo(o2.getOrder());
			}
		});
		dataGrid.addColumn(rankColumn, "Original Step Ordering");

		//set size of the data grid
		dataGrid.setRowCount(dataList.size(), false);

		// Push the data into the widget.
		dataGrid.setRowData(0, dataList);
		dataGrid.setWidth("100%");		
		dataGrid.setHeight("100%");

		dataGrid.setVisible(false);
		//add the panels together

		ScrollPanel scroller = new ScrollPanel();
		scroller.setHeight("100%");
		scroller.add(dataGrid);
		add(scroller);
	}

	public void clearData(){
		dataList = new ArrayList<ResultModel>();

		//add data to the list in the provider
		List<ResultModel> list = dataProvider.getList();
		list.removeAll(list);
		for (ResultModel results : dataList) {
			list.add(results);
		}
		dataGrid.setRowCount(dataList.size(), false);
		dataGrid.setVisibleRange(0, dataList.size());
		// Push the data into the widget.
		dataGrid.setRowData(0, dataList); 
		dataGrid.setVisible(false);
	}
	public void setListContent(List<String> titles, List<Double> values) {

		dataList = new ArrayList<ResultModel>();
		for(int i = 0; i<titles.size(); i++){
			String name = titles.get(i);	
			double value = values.get(i);	
			value = value/1000000;
			//format the value so that we don't lose any info when displayed in the table
			NumberFormat fmt = NumberFormat.getFormat("###.#################");
			String formatted = fmt.format(value);
			ResultModel rModel = new ResultModel(name, value, formatted, i+1);
			dataList.add(rModel);
		}

		//add data to the list in the provider
		List<ResultModel> list = dataProvider.getList();
		list.removeAll(list);
		for (ResultModel results : dataList) {
			list.add(results);
		}
		dataGrid.setRowCount(dataList.size(), false);
		dataGrid.setVisibleRange(0, dataList.size());
		// Push the data into the widget.
		dataGrid.setRowData(0, dataList); 
		dataGrid.setVisible(true);
	}
}
